/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.mzk.kramerius.rukopisyimport.cli;

import cz.mzk.kramerius.commons.Model;
import cz.mzk.kramerius.rukopisyimport.ManuscriptCollection;
import cz.mzk.kramerius.rukopisyimport.ManuscriptData;
import cz.mzk.kramerius.rukopisyimport.ManuscriptsConverter;
import java.io.File;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Martin Řehánek (rehan at mzk.cz)
 */
public class CreateFoxmlFilesAction implements Action {

    private static final Logger logger = Logger.getLogger(CreateFoxmlFilesAction.class.getName());
    private final ManuscriptCollection collection;
    private final String signature;
    private final String monographUuid;
    private final Properties properties;
    private final boolean inImageServer;
    private final boolean createInternalParts;
    private String monographPid = null;
    private final String modelName;

    public CreateFoxmlFilesAction(ManuscriptCollection collection, String signature, String monographUuid, Properties properties, boolean inImageServer, boolean createInternalParts, String modelName) {
        this.collection = collection;
        this.signature = signature;
        this.monographUuid = monographUuid;
        this.properties = properties;
        this.inImageServer = inImageServer;
        this.createInternalParts = createInternalParts;
        this.modelName = modelName;
    }

    public ActionResult run(ActionResult previousActionResult) {
        try {
            if (modelName == null) {
                throw new Exception("empty model");
            }
            ManuscriptData data = getManuscriptData(collection, signature, inImageServer);
            ManuscriptsConverter converter = new ManuscriptsConverter(properties);
            monographPid = converter.convertToFoxmlSet(data, createInternalParts, monographUuid, new Model(modelName));
        } catch (Throwable e) {
            logger.log(Level.SEVERE, null, e);
            return new ActionResult(false, monographPid);
        }
        return new ActionResult(true, monographPid);
    }

    private ManuscriptData getManuscriptData(ManuscriptCollection collection, String signature, boolean inImageServer) {
        File tei = buildTeiFile(collection, signature);
        File marc = buildMarcFile(collection, signature);
        File log = buildLogFile(collection, signature);
        return new ManuscriptData(signature, collection, tei, marc, log, inImageServer);
    }

    private File buildTeiFile(ManuscriptCollection col, String signature) {
        return buildRecordFile(col, signature, "tei.xml");
    }

    private File buildMarcFile(ManuscriptCollection col, String signature) {
        return buildRecordFile(col, signature, "marc21.xml");
    }

    private File buildLogFile(ManuscriptCollection col, String signature) {
        return buildRecordFile(col, signature, "log.xml");
    }

    private File buildRecordFile(ManuscriptCollection col, String signature, String suffix) {
        String collectionDir = col.getDir().getAbsolutePath();
        String filePath = collectionDir + File.separator + signature + "." + suffix;
        return new File(filePath);
    }
}
